package com.crape.common.utils;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.crape.common.constants.ConstantCode;

public class StringUtil {
	
	public static String imageUrl(String imagename, int i) {
		if (imagename == null) {
			imagename = ConstantCode.default_image;
		}
		StringBuilder imagePath = new StringBuilder();
		if (i <= 0 || i > ConstantCode.imageSize.length) {
			return imagePath.append("/").append(ConstantCode.uploadPath).append(imagename).toString();
		}
		int lastIndex = imagename.lastIndexOf(".");
		if (lastIndex < 0) {
			imagename = ConstantCode.default_image;
			lastIndex = imagename.lastIndexOf(".");
		}
		String name = imagename.substring(0, lastIndex);
		String suffix = imagename.substring(lastIndex);
		return imagePath.append("/").append(ConstantCode.uploadPath).append(name).append("_").append(ConstantCode.imageSize[i-1]).append(suffix).toString();
	}
	
	public static String[] imagesUrl(String imagename, String[] imageSize) {
		if (imagename == null) {
			imagename = ConstantCode.default_image;
		}
		StringBuilder imagePath = new StringBuilder();
		if (imageSize == null || imageSize.length == 0) {
			return new String[]{imagePath.append("/").append(ConstantCode.uploadPath).append(imagename).toString()};
		}
		int lastIndex = imagename.lastIndexOf(".");
		if (lastIndex < 0) {
			imagename = ConstantCode.default_image;
			lastIndex = imagename.lastIndexOf(".");
		}
		String name = imagename.substring(0, lastIndex);
		String suffix = imagename.substring(lastIndex);
		List<String> imageList = new ArrayList<String>();
		for(String size : imageSize){
			imagePath = new StringBuilder();
			imagePath.append("/").append(ConstantCode.uploadPath).append(name).append("_").append(size).append(suffix).toString();
			imageList.add(imagePath.toString());
		}
		return imageList.toArray(new String[0]);
	}
	
	public static String getTimeStr(String time){
		if (time != null && time.lastIndexOf(".") > -1) {
			time = time.substring(0, time.lastIndexOf("."));
        }
        return time;
	}
	
	public static void main(String[] args){
		String url = imageUrl("2013063020070048_1217948263.jpg", 0);
		System.out.println(url);
		String url1 = imageUrl("2013063020070048_1217948263.jpg", 1);
		System.out.println(url1);
		String url2 = imageUrl("2013063020070048_1217948263.jpg", 2);
		System.out.println(url2);
		String url3 = imageUrl("2013063020070048_1217948263.jpg", 3);
		System.out.println(url3);
		String url4 = imageUrl("2013063020070048_1217948263.jpg", 4);
		System.out.println(url4);
	}

	public static List<String> lineInput(String str) {
		if (str == null) {
			return null;
		}
		List<String> list = new ArrayList<String>();
		String[] array = str.split("\n");
		for (String s : array) {
			if (s != null && !s.isEmpty()) {
				list.add(s.trim());
			}
		}
		return list;
	}

	public static String lineInputHTML(String str) {
		if(str==null)
			return "";
		str = str.replaceAll("\n", "<br/>");
		str = str.replaceAll(" ", "&nbsp;");
		return str;
	}

	public static String convertHTML(String str){
		if(str==null)
			return "";
		str = str.replaceAll("&", "&amp;");
		str = str.replaceAll(">", "&gt;");
		str = str.replaceAll("<", "&lt;");
		return str;
	}

	/**
	 * URL编码
	 * @param str
	 * @return
	 */
	public static String encode(String str){
		try{
			return URLEncoder.encode(str,"UTF-8");
		}catch(Exception e){
			return "";
		}
	}
	/**
	 * URL解码
	 * @param str
	 * @return
	 */
	public static String decode(String str){
		try{
			return URLDecoder.decode(str,"UTF-8");
		}catch(Exception e){
			return "";
		}
	}

}
